package yxy.game.pm2.module.godwar;

import java.util.*;

import org.redisson.api.RMap;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.godwar;
import yxy.cherry.data.bean.title;
import yxy.cherry.data.string.GameString;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.Sign;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.NotificationHandle;
import yxy.game.pm2.module.avatar.TitleModule;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;

public class GodWarAreaModule extends AreaModule {
    // bean===========================================================================

    private int dataId;
    private int evolve;// 进化等级
    private String occupantCode;// 占有者Code

    public int getDataId() {
        return dataId;
    }

    public void setDataId(int dataId) {
        this.dataId = dataId;
    }

    public int getEvolve() {
        return evolve;
    }

    public void setEvolve(int evolve) {
        this.evolve = evolve;
    }

    public String getOccupantCode() {
        return occupantCode;
    }

    public void setOccupantCode(String occupantCode) {
        this.occupantCode = occupantCode;
    }

    // func===========================================================================

    public godwar data() {
        return DataCenter.getData(getDataId(), godwar.class);
    }

    public Currency titleReward() {
        godwar data = data();
        Currency reward = Currency.create(data.getPositiontype(), data.getPositionid(), data.getPositionnum());
        return reward;
    }

    /**
     * 占领
     */
    public void occupy(String playerCode) {
        recall();
        //清除所有武魂殿的称号，重新赋予称号
        try {
            for (int i = 101; i <= 103; i++) {
                Sign.minus(playerCode, CurrencyType.CT_称号, i);
            }
        } catch (CurrencyNotEnoughException e) {
        }

        //清除对应的称号缓存
        for (int i = 101; i <= 103; i++) {
            TitleModule.remove(playerCode, String.valueOf(i));
        }

        setOccupantCode(playerCode);
        new CurrencyHandle().addCurrency(playerCode, CCReason.武魂殿, titleReward());

        title _title = DataCenter.getData(titleReward().getDataId(), title.class);
        PlayerBean playerBean = PlayerBean.get(playerCode);
        if (playerBean.getTitle() == 0) {
            try {
                if (playerBean.tryLock()) {
                    playerBean.setTitle(_title.getId());
                    playerBean.set();
                }
            } catch (Exception e) {
            } finally {
                playerBean.unlock();
            }
        }
        // 滚屏
        new NotificationHandle().notifyChatInner(playerCode, GameString.获得称号(playerBean.getName(), _title.getName()));
    }

    /**
     * 罢免
     */
    public void recall() {
        if (getOccupantCode() == null) {
            return;
        }
        try {
            new CurrencyHandle().cost(getOccupantCode(), null, titleReward());
        } catch (Exception e) {
        }
//		TitleModule.remove(getOccupantCode(), String.valueOf(data().getPositionid()));
//
//		// 撤下玩家的称号
//		PlayerBean oldBean = PlayerBean.get(getOccupantCode());
//		if (oldBean.getTitle() == titleReward().getDataId()) {
//			try {
//				if (oldBean.tryLock()) {
//					oldBean.setTitle(0);
//					oldBean.set();
//
//					new NotificationHandle().notifyPlayerTitle(oldBean);
//					new BattleHandle().powerUpdate(oldBean.getCode());
//				}
//			} catch (Exception e) {
//			} finally {
//				oldBean.unlock();
//			}
//		}
        setOccupantCode(null);
    }

    // redis==========================================================================

    static public String key(int areaId) {
        return key(areaId, "GODWAR");
    }

    static private RMap<Integer, GodWarAreaModule> touchMap(int areaId) throws RedisException {
        return RedisManager.client(redis()).getMap(key(areaId));
    }

    public void set() {
        try {
            RMap<Integer, GodWarAreaModule> map = touchMap(getAreaId());
            map.put(getDataId(), this);
        } catch (RedisException e) {
        }
    }

    static public Map<Integer, GodWarAreaModule> get(String playerCode) {
        AreaBean area = area(playerCode);
        return get(area.getAreaId());
    }

    static public Map<Integer, GodWarAreaModule> get(int areaId) {
        try {
            RMap<Integer, GodWarAreaModule> rmap = touchMap(areaId);
            if (rmap.size() <= 0) {
                Map<String, godwar> all = DataCenter.getAllData(godwar.class);
                for (godwar data : all.values()) {
                    GodWarAreaModule module = create(areaId, data);
                    rmap.put(module.getDataId(), module);
                }
            }
            return rmap.readAllMap();
        } catch (RedisException e) {
            e.printStackTrace();
        }
        return new HashMap<>();
    }

    public static GodWarAreaModule get(String playerCode, int dataId) {
        AreaBean area = area(playerCode);
        return get(area.getAreaId(), dataId);
    }

    static public GodWarAreaModule get(int areaId, int dataId) {
        try {
            RMap<Integer, GodWarAreaModule> map = touchMap(areaId);
            if (!map.containsKey(dataId)) {
                godwar data = DataCenter.getData(dataId, godwar.class);
                if (data == null) {
                    return null;
                }
                GodWarAreaModule module = create(areaId, data);
                map.put(module.getDataId(), module);
            }
            return map.get(dataId);
        } catch (RedisException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static GodWarAreaModule create(int areaId, godwar data) {
        GodWarAreaModule module = new GodWarAreaModule();
        module.setAreaId(areaId);
        module.setDataId(data.getId());
        return module;
    }

    // lock==========================================================================

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getAreaId()));
    }
}
